// BESEconomy

log using BESEconomy.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\BES2019_W26_v26.0.dta" // Fieldhouse, E., J. Green, G. Evans, J. Mellon, C. Prosser, J. Bailey, J. Griffiths, S. Perrett. (2023) British Election Study Internet Panel Waves 1-26. DOI: 10.5255/UKDA-SN-8202-2// Accessed on March 11 2025.

// Social justice scale
* Clean "don't know" responses 
foreach var in cwTrans cwAuthors cwLanguage cwTraining {
    replace `var' = . if `var' == 9999
}

*Reverse variable so social justice coded high
foreach var in cwLanguage cwTraining {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in cwTrans cwAuthors rcwLanguage rcwTraining {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in scwTrans scwAuthors srcwLanguage srcwTraining {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in scwTrans scwAuthors srcwLanguage srcwTraining {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Economic variables
* Clean "don't know" responses
foreach var in econPersonalRetro econGenRetro riskPoverty riskUnemployment changeEconomy changeCostLive cutsTooFarLocal cutsTooFarNational worryEconSecurity neverPrivSchl homeFinance inheritMoney homeOwn2 homeAmtb savings savingsAmtb borrowEssentials smallEmergency2_5 selfOccSupervise selfOccEmployees {
    replace `var' = . if `var' == 9999
}

replace p_gross_household = . if inlist(p_gross_household, 16, 17)
replace p_socgrade = . if inlist(p_socgrade, 7, 8)

gen nothomeowner=.
replace nothomeowner=1 if inrange(homeOwn2, 3, 6)
replace nothomeowner=0 if inlist(homeOwn2, 1, 2)

gen unemployed=.
replace unemployed=1 if p_work_stat==6
replace unemployed=0 if inrange(p_work_stat, 1, 3) 

*Reverse variables so bad conditions coded low
foreach var in riskPoverty riskUnemployment changeCostLive cutsTooFarLocal neverPrivSchl cutsTooFarNational worryEconSecurity nothomeowner borrowEssentials smallEmergency2_5 unemployed p_socgrade {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' - `var'
}

// Correlations
pwcorr SocJusValues rriskPoverty rriskUnemployment rchangeCostLive rcutsTooFarLocal rneverPrivSchl rcutsTooFarNational rworryEconSecurity rnothomeowner rborrowEssentials rsmallEmergency2_5 runemployed rp_socgrade econPersonalRetro econGenRetro changeEconomy homeFinance inheritMoney homeAmtb savings savingsAmtb selfOccSupervise selfOccEmployees p_gross_household [aweight=wt], sig

log close

